//
//  AccountAboutVC.m
//  ImGuider
//
//  Created by llt on 2017/6/7.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "AccountAboutVC.h"
#import <WXApi.h>
#import "AccountCell.h"

@interface AccountAboutVC ()
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UILabel *copyrightLabel;
@property (nonatomic, strong) NSMutableArray *dataSource;


@end

@implementation AccountAboutVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
}

- (void)setup {
    
    self.title = MYLocalizedString(@"SETTING_ABOUTUS", nil);
    
    self.view.backgroundColor = kBGColor;
    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    // 获取App的版本号
    NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    
    self.versionLabel.text = [@"ImGuider V" stringByAppendingString:appVersion];
    
    self.dataSource = [NSMutableArray array];
    
    for (NSDictionary *dict in [AppManager sharedManager].version.info.abs) {
        
        NSDictionary *myDict = @{@"account":dict[@"abaccount"], @"type":dict[@"abtype"], @"name":dict[@"abname"]};
        [self.dataSource addObject:myDict];
    }
    
    
    CGRect tbfFrame = self.copyrightLabel.frame;
    tbfFrame.size.height = SCREEN_HEIGHT - (64 + 9) * self.dataSource.count - kNavigationBarHeight - 171;
    
    if (tbfFrame.size.height < 20) {
        tbfFrame.size.height = 20;
    }
    
    self.copyrightLabel.frame = tbfFrame;

}
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dict = self.dataSource[indexPath.row];
    
    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if ([dict[@"type"] isEqualToString:@"web"]) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.contentLableTrailing.constant = 5;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.contentLableTrailing.constant = 15;
    }
    
    [cell configWithDict:dict];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dict = self.dataSource[indexPath.row];
    
    NSString *contact = dict[@"account"];
    
//    if ([dict[@"type"] isEqualToString:@"wechat"]) {
//        
//        //    跳转到微信公众号，首先需要到微信开发平台，绑定对应的公众号
//        if ([WXApi isWXAppInstalled]) {
//            JumpToBizProfileReq *req = [[JumpToBizProfileReq alloc]init];
//            req.username = contact;
//            req.extMsg = @"";
//            req.profileType = 0;
//            [WXApi sendReq:req];
//            
//        } else {
//            
//            [IGUtils showToast:@"打开微信失败"];
//        }
//        
//        return;
//    }
//    
//    if ([dict[@"type"] isEqualToString:@"sina"]) {
//        
//        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"sinaweibo://userinfo?uid=%@",contact]];
//        // 如果已经安装了这个应用,就跳转
//        if ([[UIApplication sharedApplication] canOpenURL:url]) {
//            [[UIApplication sharedApplication] openURL:url];
//        } else {
//            
//            [IGUtils showToast:@"打开微博失败"];
//        }
//        
//        return;
//    }
    
    if ([dict[@"type"] isEqualToString:@"web"]) {
        
        NSURL *url = [NSURL URLWithString:contact];
//        SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:url];
//        [self presentViewController:safariVC animated:YES completion:nil];
        
        
//        CGRect viewFrame = self.view.bounds; //1
//        WKWebView *webView = [[WKWebView alloc] initWithFrame:viewFrame]; //2
//        NSURL *url = [NSURL URLWithString:contact]; //3
//        NSURLRequest *request = [NSURLRequest requestWithURL:url]; //4
//        [webView loadRequest:request]; //5
//        
//        BaseViewController *baseView = [[BaseViewController alloc] init];
//        [baseView.view addSubview:webView];
//        
//        [self.navigationController pushViewController:baseView animated:YES];
        
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
        
        return;
    }
}

@end
